package com.example.supernote.ui.note

import android.net.Uri
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.ArrowCircleLeft
import androidx.compose.material.icons.filled.ArrowCircleRight
import androidx.compose.material.icons.filled.Save
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.dp
import com.example.supernote.R
import com.example.supernote.model.note.commands.AbstractCommand
import com.example.supernote.model.note.context.Context
import com.example.supernote.ui.components.UniformButton
import com.example.supernote.ui.components.UniformToast


@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun NoteScreen(
    uiState: NoteUiState,
    onBack: () -> Unit,
    editContext: (context: TextFieldValue) -> Unit,
    editTitle: (title: String) -> Unit,
    changeEditMode: () -> Unit,
    execute: (command: AbstractCommand, context: Context) -> Unit,
    saveFile: () -> Unit,
    changeOperation: (choice: String) -> Unit,
    apiByPrompt: ((String) -> Unit, Map<String, String>) -> Unit,
    imageGenerateApiByPrompt: ((String) -> Unit, Map<String, String>) -> Unit,
    imageOCRApi: ((String) -> Unit, String, String) -> Unit,
    translateTextApi: ((String) -> Unit, Map<String, String>) -> Unit,
    audioApi: (updateTemplate: (String) -> Unit, updateProgress: (Float) -> Unit, Uri, android.content.Context) -> Unit,
    redo: () -> Unit,
    undo: () -> Unit,
    snackbarHostState: SnackbarHostState,
) {
    val edit = uiState.editMode
    val toastContext = LocalContext.current

    Scaffold(
        snackbarHost = { SnackbarHost(hostState = snackbarHostState) },
        topBar = {
            TopAppBar(
                navigationIcon = {
                    UniformButton(
                        onClick = onBack,
                        imageVector = Icons.Filled.ArrowBack,
                        modifier = Modifier
                            .padding(start = 5.dp),
                        contentDescription = stringResource(R.string.note_back)
                    )
                },
                actions = {
                    if (edit) {
                        UniformButton(
                            onClick = undo,
                            modifier = Modifier
                                .padding(start = 5.dp),
                            imageVector = Icons.Filled.ArrowCircleLeft,
                            contentDescription = stringResource(R.string.note_withdraw),
                            text = stringResource(R.string.note_withdraw)
                        )
                        UniformButton(
                            onClick = redo,
                            modifier = Modifier
                                .padding(start = 5.dp),
                            imageVector = Icons.Filled.ArrowCircleRight,
                            contentDescription = stringResource(R.string.note_recovery),
                            text = stringResource(R.string.note_recovery)
                        )
                    }
                    val t = stringResource(R.string.note_save_success)
                    UniformButton(
                        onClick = {
                            saveFile()
                            UniformToast(toastContext, t)
                        },
                        modifier = Modifier
                            .padding(5.dp),
                        imageVector = Icons.Filled.Save,
                        contentDescription = stringResource(R.string.note_save),
                        text = "保存"
                    )
                },
                title = { Text(text = "") },
            )
        }
    ) { innerPadding ->
        NoteContent(
            modifier = Modifier.padding(innerPadding),
            uiState = uiState,
            editContext = editContext,
            editTitle = editTitle,
            saveFile = saveFile,
            changeEditMode = changeEditMode,
            execute = execute,
            changeOperation = changeOperation,
            apiByPrompt = apiByPrompt,
            imageGenerateApiByPrompt = imageGenerateApiByPrompt,
            imageOCRApi = imageOCRApi,
            translateTextApi = translateTextApi,
            audioApi = audioApi
        )

    }
}